package com.flyme.common.core.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.flyme.common.core.exception.OpenMessageException;
import com.flyme.common.core.model.ResultBody;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Objects;

/**
 * API 断言
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ApiAssert {

    /**
     * obj1 eq obj2
     *
     * @param obj1
     * @param obj2
     * @param j
     */
    public static void eq(String info, Object obj1, Object obj2) {
        if (!Objects.equals(obj1, obj2)) {
            failure(info);
        }
    }

    public static void isTrue(String info, boolean condition) {
        if (!condition) {
            failure(info);
        }
    }

    public static void isFalse(String info, boolean condition) {
        if (condition) {
            failure(info);
        }
    }


    public static void gtzero(String info, int n) {
        if (n == 0) {
            failure(info);
        }
    }
    public static void lezero(String info, int n) {
        if (n > 0) {
            failure(info);
        }
    }

    public static void isNull(String info, Object... conditions) {
        if (com.flyme.common.core.utils.ObjectUtils.isNotNull(conditions)) {
            failure(info);
        }
    }

    public static void notNull(String info, Object... conditions) {
        if (com.flyme.common.core.utils.ObjectUtils.isNull(conditions)) {
            failure(info);
        }
    }

    /**
     * <p>
     * 失败结果
     * </p>
     *
     * @param j 异常错误码
     */
    private static void failure(ResultBody j) {
        throw new OpenMessageException(j.getInfo());
    }

    /**
     * <p>
     * 失败结果
     * </p>
     */
    public static void failure(String info) {
        throw new OpenMessageException(info);
    }

    public static void notEmpty(ResultBody j, Object[] array) {
        if (com.flyme.common.core.utils.ObjectUtils.isEmpty(array)) {
            failure(j);
        }
    }

    /**
     */
    public static void noNullElements(ResultBody j, Object[] array) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    failure(j);
                }
            }
        }
    }

    public static void notEmpty(ResultBody j, Collection<?> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            failure(j);
        }
    }


}
